package com.polaris.live.widget.refresh

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.RelativeLayout
import androidx.core.content.ContextCompat
import com.polaris.live.databinding.WidgetItemErrorBinding

/**
 * 错误显示
 *
 * @author Created by lucas on 2023/10/16 19:38
 * @since 1.0.0
 */
class ErrorView(context: Context, attrs: AttributeSet? = null) : RelativeLayout(context, attrs) {

    private val mBinding = WidgetItemErrorBinding.inflate(LayoutInflater.from(context), this)

    protected fun initView() {
        isClickable = true
    }

    /**
     * 设置文本
     *
     * @param text 文本
     */
    fun setText(text: String) {
        mBinding.text.text = text
    }

    /**
     * 设置文本颜色
     *
     * @param color 颜色
     */
    fun setTextColor(color: Int) {
        mBinding.text.setTextColor(color)
    }

    /**
     * 设置图片
     *
     * @param resId 图片资源Id
     */
    fun setTextDrawable(resId: Int, padding: Int) {
        val draw = if (resId == 0) {
            null
        } else {
            ContextCompat.getDrawable(context, resId)
        }
        if (draw == null) {
            mBinding.text.compoundDrawablePadding = 0
            mBinding.text.setCompoundDrawables(null, null, null, null)
            return
        }
        draw.setBounds(0, 0, draw.minimumWidth, draw.minimumHeight)
        mBinding.text.compoundDrawablePadding = padding
        mBinding.text.setCompoundDrawables(null, draw, null, null)
    }
}